const mysql = require('mysql')
const pool = mysql.createPool({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    // port: 3306,
    database: 'goods',
    // 是否允许多条sql语句同时执行（默认false）
    multipleStatements: true,
    // connectionLimit:20,
})

module.exports = {
    // 查询
    query:function(sql){
        return new Promise(function(resolve,reject){
            pool.query(sql,function(err,result){
                if(err){
                    reject(err)
                }else{
                    resolve(result)
                }
            })
        })
    },
    // 注册 
    reg: function(username, password){
        let sql = `insert into users(username,password) values('${username}','${password}')`
        return new Promise(function(resolve,reject){
            pool.query(sql,function(err,result){
                if(err){
                    reject(err)
                }else{
                    resolve(result.insertId ? true : false)
                }
            })
        })
    },
    // 登录
    login: function(username, password){
        let sql = `select * from users where username='${username}' and password='${password}'`
        return new Promise(function(resolve,reject){
            pool.query(sql,function(err,result){
                if(err){
                    reject(err)
                }else{
                    resolve(result.length>0 ? true : false)
                }
            })
        })
    },
    // 检测
    checked: function(username){
        let sql = `select id from users where username='${username}'`
        return new Promise(function(resolve,reject){
            pool.query(sql,function(err,result){
                if(err){
                    reject(err)
                }else{
                    resolve(result.length>0 ? true : false)
                }
            })
        })
    }
}